package com.feisuanyz.service.impl;

import com.feisuanyz.dto.EventQuery;
import com.feisuanyz.dto.RestResult;
import com.feisuanyz.entity.EventManagement;
import com.feisuanyz.entity.OperationLog;
import com.feisuanyz.repository.EventManagementRepository;
import com.feisuanyz.repository.OperationLogRepository;
import com.feisuanyz.service.EventService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class EventServiceImpl implements EventService {

    @Autowired
    private EventManagementRepository eventManagementRepository;

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public RestResult<Void> createEvent(EventQuery eventQuery) {
        try {
            EventManagement eventManagement = modelMapper.map(eventQuery, EventManagement.class);
            eventManagement.setCreateTime(new Date());
            eventManagementRepository.save(eventManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setEventId(eventManagement.getEventId());
            operationLog.setOperationType(OperationLog.OperationType.CREATE);
            operationLog.setDetails("Created event with ID: " + eventManagement.getEventId());
            operationLog.setCreateBy(eventQuery.getCreateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to create event", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> editEvent(EventQuery eventQuery) {
        try {
            EventManagement existingEvent = eventManagementRepository.findById(eventQuery.getEventId())
                    .orElseThrow(() -> new RuntimeException("Event not found"));

            if (eventQuery.getTitle() != null) {
                existingEvent.setTitle(eventQuery.getTitle());
            }
            if (eventQuery.getDescription() != null) {
                existingEvent.setDescription(eventQuery.getDescription());
            }
            if (eventQuery.getLocation() != null) {
                existingEvent.setLocation(eventQuery.getLocation());
            }
            if (eventQuery.getVenue() != null) {
                existingEvent.setVenue(eventQuery.getVenue());
            }
            if (eventQuery.getEventDate() != null) {
                existingEvent.setEventDate(eventQuery.getEventDate());
            }
            if (eventQuery.getEventTime() != null) {
                existingEvent.setEventTime(eventQuery.getEventTime());
            }
            existingEvent.setUpdateBy(eventQuery.getUpdateBy());

            eventManagementRepository.save(existingEvent);

            OperationLog operationLog = new OperationLog();
            operationLog.setEventId(existingEvent.getEventId());
            operationLog.setOperationType(OperationLog.OperationType.EDIT);
            operationLog.setDetails("Updated event with ID: " + existingEvent.getEventId());
            operationLog.setCreateBy(eventQuery.getUpdateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to edit event", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> deleteEvent(Integer eventId, Integer deleteBy) {
        try {
            EventManagement eventManagement = eventManagementRepository.findById(eventId)
                    .orElseThrow(() -> new RuntimeException("Event not found"));

            eventManagementRepository.delete(eventManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setEventId(eventId);
            operationLog.setOperationType(OperationLog.OperationType.DELETE);
            operationLog.setDetails("Deleted event with ID: " + eventId);
            operationLog.setCreateBy(deleteBy);
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to delete event", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }
}